package com.sikaryofficial.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.annotation.Repeat;
import com.sikaryofficial.backend.domain.dto.req.prize.LotteryReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityBrandSpecialListReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityRecommendListReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityReserveListReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityReserveReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityResultListReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityResultMapReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityResultReq;
import com.sikaryofficial.backend.domain.dto.resp.productactivity.ActivityDTO;
import com.sikaryofficial.backend.domain.dto.resp.productactivity.ActivityReserveDTO;
import com.sikaryofficial.backend.domain.dto.resp.productactivity.ActivityResultDTO;
import com.sikaryofficial.backend.manager.ActivityManager;
import com.sikaryofficial.backend.manager.ActivityResultManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.core.web.page.PageDomain;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 活动Controller
 *
 * @author qinjinyuan
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/activity")
@Api(tags = "V3.5-活动")
public class ActivityController extends BaseController {

    @Autowired
    private ActivityManager activityManager;

    @Autowired
    private ActivityResultManager activityResultManager;

    /**
     * V2.5.0-品牌专栏-活动奖品列表
     */
    @PostMapping("/activityListByBrandSpecial")
    @ApiOperation("V3.5-品牌专栏-活动奖品列表")
    @Repeat
    public PageResult<ActivityDTO> activityListByBrandSpecial(@Validated @RequestBody ActivityBrandSpecialListReq req) {
        IPage<ActivityDTO> page = activityManager.activityListByBrandSpecial(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }
    /**
     * 首页-活动推荐列表
     */
    @PostMapping("/recommend")
    @ApiOperation("V3.5-首页-活动推荐列表")
    @Repeat
    public PageResult<ActivityDTO>  recommendList(@Validated @RequestBody ActivityRecommendListReq req) {
        IPage<ActivityDTO> page = activityManager.recommendList(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 获取首页活动奖品列表
     */
    @PostMapping("/activityPrizeList")
    @ApiOperation("获取活动奖品列表")
    @Repeat
    public PageResult<ActivityDTO> activityPrizeList(@Validated @RequestBody ActivityRecommendListReq req) {
        IPage<ActivityDTO> page = activityManager.getActivityPrizeList(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 活动预约
     */
    @Log(title = "活动预约新增", businessType = BusinessType.INSERT)
    @PostMapping("/reserve/add")
    @ApiOperation("V3.5-活动预约新增")
    public R<Boolean> reserveSave(@RequestBody ActivityReserveReq req) {
        return R.ok(activityManager.reserveSave(req));
    }

    /**
     * 线下活动预约新增
     */
    @PostMapping("/reserve/offline/add")
    @ApiOperation("V3.5-线下活动预约新增")
    public R<Boolean> reserveOfflineSave(@RequestBody ActivityReserveReq req) {
        return R.ok(activityManager.reserveOfflineSave(req));
    }

    /**
     * 活动抢购
     */
    @Log(title = "活动抢购", businessType = BusinessType.INSERT)
    @GetMapping("/attend/{activityId}")
    @ApiOperation("V3.5-活动抢购")
    public R<Boolean> attendSave(
            @ApiParam(value = "活动id", required = true)
            @PathVariable("activityId") Long activityId) {
        return R.ok(activityManager.attendSave(activityId));
    }

    /**
     * 活动转盘及刮刮乐
     */
    @PostMapping("/lottery")
    @ApiOperation("V3.5-活动抽奖-幸运转盘或者刮刮乐")
    public R<Long> lotterySave(@Validated @RequestBody LotteryReq req) {
        return R.ok(activityManager.lotterySave(req));
    }

    /**
     * 活动抢购校验
     */
    @GetMapping("/lottery/verify/{activityId}")
    @ApiOperation("V3.5-活动抢购-校验")
    public R<Boolean> lotteryVerify(
            @ApiParam(value = "活动id", required = true)
            @PathVariable("activityId") Long activityId) {
        return R.ok(activityManager.lotteryVerify(activityId));
    }

    /**
     * 活动可参与次数
     */
    @PostMapping("/attendNum")
    @ApiOperation("V3.5-活动可参与次数")
    public R<Integer> attendNum(@Validated @RequestBody LotteryReq req) {
        return R.ok(activityManager.attendNum(req));
    }

    /**
     * 活动是否预约
     */
    @GetMapping("/reserve/{activityId}")
    @ApiOperation("活动是否预约: data 0-未预约 1-已预约 2-已过期(抢购)")
    public R<Integer> hasReserve(@PathVariable Long activityId) {
        return R.ok(activityManager.hasReserve(activityId));
    }

    /**
     * 活动抢购结果
     */
    @GetMapping("/attendResult/{activityId}")
    @ApiOperation("活动结果-轮询接口")
    public R<Boolean> attendResult(@PathVariable("activityId") Long activityId) {
        return R.ok(activityManager.attendResult(activityId));
    }

    /**
     * 我的预约列表
     */
    @PostMapping("/reserve/list")
    @ApiOperation("我的预约列表")
    public PageResult<ActivityReserveDTO> reserveList(@Validated @RequestBody ActivityReserveListReq reserveListReq) {
        IPage<ActivityReserveDTO> page = activityManager.reserveList(reserveListReq);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 我的中奖结果列表
     */
    @PostMapping("/my/awardsListPage")
    @ApiOperation("我的中奖列表")
    public PageResult<ActivityReserveDTO> myAwardsList(@Validated @RequestBody PageDomain req) {
        IPage<ActivityReserveDTO> page = activityManager.myAwardsList(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }
    /**
     * 中奖结果地址更新
     */
    @PostMapping("/awards/updateAddress")
    @ApiOperation("中奖结果地址更新")
    public R<Boolean> updateAwards(@Validated @RequestBody ActivityResultReq req) {
        return R.ok(activityManager.updateAwards(req));
    }
    /**
     * 获奖名单
     *
     */
    @PostMapping("/awardsListPage")
    @ApiOperation("获奖名单")
    public PageResult<ActivityResultDTO> awardsListPage(@Validated @RequestBody ActivityResultListReq req) {
        // 查询获奖列表
        IPage<ActivityResultDTO> page = activityManager.publishAwardsList(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 获奖名单更新
     *
     */
    @PostMapping("/awardsListUpdate")
    @ApiOperation("获奖名单更新（后台）")
    public R<Boolean> awardsListUpdate(@RequestBody ActivityResultMapReq req) {
        return R.ok(activityManager.awardsListUpdate(req.getList()));
    }

    /**
     * 自动发送获奖邮件
     * @return
     */
    @GetMapping("/autoSendAwardsEmailNotice")
    @ApiOperation("自动发送获奖邮件")
    public R<Boolean> autoSendAwardsEmailNotice() {
        // 查询获奖列表
        return  R.ok(activityResultManager.autoSendAwardsEmailNotice());
    }
}
